package logic

import (
	"context"
	"fmt"
	"patient/patientrpc/models/mysql"

	"patient/patientrpc/internal/svc"
	"patient/patientrpc/patientrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserRegisterLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUserRegisterLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserRegisterLogic {
	return &UserRegisterLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UserRegisterLogic) UserRegister(in *patientrpc.UserRegisterRequest) (*patientrpc.UserRegisterResponse, error) {
	// todo: add your logic here and delete this line

	var users mysql.User

	mysql.DB.Debug().Where("account=?", in.Account).Find(&users)

	if users.ID != 0 {
		return nil, fmt.Errorf("账号已存在不允许再被注册")
	}
	user := mysql.User{

		Account:  in.Account,
		Password: in.Password,
	}
	mysql.DB.Debug().Create(&user)
	return &patientrpc.UserRegisterResponse{
		Id: int64(user.ID),
	}, nil
}
